<template>
  <div class="app-container">
    <el-card>
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
    <!--表单组件
    <eForm ref="form" :uploadImgApi="uploadImgApi" :baseApi="baseApi"/>-->
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="名称" width="165"/>
      <el-table-column prop="description" label="描述" width="255"/>
      <el-table-column prop="addTime" label="创建时间" width="255"/>

      <el-table-column  label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
      <!--分页组件-->
      <pagination />
   </el-card>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form
        ref="form"
        :model="form"
        size="small"
        label-width="80px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;"/>
        </el-form-item>

        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" style="width: 370px;"/>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="text"
          @click="crud.cancelCU"
        >
          取消
        </el-button>
        <el-button
          :loading="crud.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { del } from '@/api/ceamCategory'
import crudCategory from '@/api/ceamCategory'
import eHeader from './header'
import CRUD, { presenter, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

// crud交由presenter持有
const crud = CRUD({
  title: '分类',
  url: 'api/category',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudCategory }
})
const defaultForm = { name: null, carNum: null, img: '', money: null,  count: null }
export default {
  components: { eHeader, crudOperation, pagination, udOperation },
  mixins: [presenter(crud),form(defaultForm)],
  data() {
    return {
      headers: {
        'Authorization': getToken()
      },
      delLoading: false,
      permission: {
        add: ['admin', 'category:add'],
        edit: ['admin', 'category:edit'],
        del: ['admin', 'category:del']
      }
    }
  },
  created() {
    // this.$nextTick(() => {
    //   this.init()
    // })
  },
  computed: {
    ...mapGetters([
      'uploadImgApi',
      'baseApi'
    ])
  },
  methods: {
    checkPermission,
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {
      this.form.id = null;
      return true
    }
  }
}
</script>

<style scoped>

</style>
